package com.saifan.wyy_ov.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.ui.onlishop.MyOrderActivity;
import com.saifan.wyy_ov.ui.onlishop.OnlineShopActivity;

import com.saifan.wyy_ov.ui.onlishop.PayActivity;
import com.saifan.wyy_ov.ui.onlishop.SubmitOrderActivity;
import com.saifan.wyy_ov.ui.property.CostPayActivity;
import com.saifan.wyy_ov.ui.property.FeeQueryActivity;
import com.saifan.wyy_ov.utils.ActivitysManager;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import net.sourceforge.simcpux.Constants;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private static final String TAG = "WXPayEntryActivity";
    private Toolbar toolbar;
    private IWXAPI api;
    private int result = -1;
    private android.widget.ImageView resultimg;
    private android.widget.TextView resulttv;
    private android.widget.Button seeorder;
    private android.widget.Button toonlineshop;
    private boolean isFromBill = false;
    private boolean isFromCost = false;
    private TextView tip;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        isFromBill = ActivitysManager.getManager().currentActivity() instanceof FeeQueryActivity;
        isFromCost = ActivitysManager.getManager().currentActivity() instanceof CostPayActivity;
        ActivitysManager.getManager().removeActivityClass(PayActivity.class);
        super.onCreate(savedInstanceState);
        if (isFromBill || isFromCost) {
            setContentView(R.layout.activity_bill_pay);
        } else {
            setContentView(R.layout.activity_pay_result);
        }
        setViews();
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
//        ActivitysManager.getManager().addActivity(this);
        if (isFromCost){
            ActivitysManager.getManager().removeActivityClass(CostPayActivity.class);
        }
        if (isFromBill){
            ActivitysManager.getManager().removeActivityClass(FeeQueryActivity.class);
        }
    }

    private void setViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        this.resulttv = (TextView) findViewById(R.id.result_tv);
        this.resultimg = (ImageView) findViewById(R.id.result_img);
        result = getIntent().getIntExtra("pay_result", -1);
        if (result == 0) {
            resultimg.setImageResource(R.mipmap.pay_succeed_2);
            resulttv.setText("支付成功");
            if (isFromBill || isFromCost)
                ((TextView) findViewById(R.id.tip)).setVisibility(View.VISIBLE);
        }
        if (isFromBill || isFromCost) {
            findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isFromBill) {
                        Intent intent = new Intent(WXPayEntryActivity.this, FeeQueryActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("payResult", result);
                        startActivity(intent);
                    }
                    finish();
                }
            });
            return;
        }
        this.toonlineshop = (Button) findViewById(R.id.to_onlineshop);
        this.seeorder = (Button) findViewById(R.id.see_order);
        seeorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent intent = new Intent(WXPayEntryActivity.this, MyOrderActivity.class);
                startActivity(intent);
//                ActivitysManager.getManager().removeActivityClass(PayActivity.class);
                finish();
            }
        });
        toonlineshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(WXPayEntryActivity.this, OnlineShopActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
//                ActivitysManager.getManager().removeActivityClass(PayActivity.class);
                finish();

            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {
                result = 0;
                if (isFromBill || isFromCost) {
                    ((TextView) findViewById(R.id.tip)).setVisibility(View.VISIBLE);
                    resultimg.setImageResource(R.mipmap.pay_succeed_2);
                } else
                    resultimg.setImageResource(R.mipmap.pay_succeed_2);
                resulttv.setText("支付成功");
            } else {
                result = -1;
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isFromCost || isFromBill && result == 0) {
            if (isFromBill) {
                Intent intent = new Intent(WXPayEntryActivity.this, FeeQueryActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("payResult", result);
                startActivity(intent);
            }
            finish();
            return;
        }
        if (result == 0) {
            Intent intent = new Intent(WXPayEntryActivity.this, OnlineShopActivity.class);
            startActivity(intent);
            finish();
        }
    }
}